import 'package:flutter/material.dart';
import 'dart:async';
import '../constants/colors.dart';
import '../widgets/gradient_background.dart';
import '../widgets/app_button.dart';
import '../widgets/app_card.dart';

class TimeDivinationPage extends StatefulWidget {
  const TimeDivinationPage({Key? key}) : super(key: key);

  @override
  State<TimeDivinationPage> createState() => _TimeDivinationPageState();
}

class _TimeDivinationPageState extends State<TimeDivinationPage> {
  late DateTime _currentTime;
  late Timer _timer;
  
  @override
  void initState() {
    super.initState();
    _currentTime = DateTime.now();
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        _currentTime = DateTime.now();
      });
    });
  }
  
  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }
  
  String _formatTime() {
    return '${_currentTime.hour.toString().padLeft(2, '0')}:${_currentTime.minute.toString().padLeft(2, '0')}:${_currentTime.second.toString().padLeft(2, '0')}';
  }
  
  String _formatDate() {
    const List<String> weekdays = ['一', '二', '三', '四', '五', '六', '日'];
    return '${_currentTime.year}年${_currentTime.month}月${_currentTime.day}日 星期${weekdays[_currentTime.weekday - 1]}';
  }
  
  void _generateHexagram() {
    // 在实际应用中，这里会根据当前时间生成卦象
    // 跳转到结果页面
    Navigator.pushNamed(
      context,
      '/divination-result',
      arguments: {'type': 'time', 'timestamp': _currentTime.millisecondsSinceEpoch},
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GradientBackground(
        child: SafeArea(
          child: Column(
            children: [
              // 顶部栏
              Padding(
                padding: const EdgeInsets.symmetric(
                  horizontal: 20,
                  vertical: 15,
                ),
                child: Row(
                  children: [
                    GestureDetector(
                      onTap: () => Navigator.pop(context),
                      child: const Text(
                        '←',
                        style: TextStyle(
                          fontSize: 24,
                          color: Colors.black,
                        ),
                      ),
                    ),
                    Expanded(
                      child: Center(
                        child: Text(
                          '时间起卦',
                          style: Theme.of(context).textTheme.displayMedium,
                        ),
                      ),
                    ),
                    const SizedBox(width: 24),
                  ],
                ),
              ),
              
              // 主体内容
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(24),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Column(
                        children: [
                          const SizedBox(height: 32),
                          Text(
                            '时间起卦',
                            style: Theme.of(context).textTheme.displayLarge,
                          ),
                          const SizedBox(height: 16),
                          Text(
                            '系统将读取当前时间，生成专属卦象。',
                            style: Theme.of(context).textTheme.bodyLarge?.copyWith(
                              color: Colors.black.withOpacity(0.7),
                            ),
                          ),
                          const SizedBox(height: 48),
                          
                          // 时间显示卡片
                          AppCard(
                            padding: const EdgeInsets.symmetric(vertical: 32, horizontal: 24),
                            child: Column(
                              children: [
                                Text(
                                  '当前北京时间',
                                  style: Theme.of(context).textTheme.bodyLarge?.copyWith(
                                    color: Colors.black.withOpacity(0.6),
                                  ),
                                ),
                                const SizedBox(height: 8),
                                ShaderMask(
                                  shaderCallback: (bounds) => LinearGradient(
                                    colors: [
                                      AppColors.primaryGradientStart,
                                      AppColors.primaryGradientEnd,
                                    ],
                                  ).createShader(bounds),
                                  child: Text(
                                    _formatTime(),
                                    style: const TextStyle(
                                      fontSize: 48,
                                      fontWeight: FontWeight.bold,
                                      color: Colors.white,
                                    ),
                                  ),
                                ),
                                const SizedBox(height: 16),
                                Text(
                                  _formatDate(),
                                  style: Theme.of(context).textTheme.bodyLarge,
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                      
                      // 底部按钮
                      Column(
                        children: [
                          AppPrimaryButton(
                            text: '立即起卦',
                            onPressed: _generateHexagram,
                          ),
                          const SizedBox(height: 16),
                          AppSecondaryButton(
                            text: '取消',
                            onPressed: () => Navigator.pop(context),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}